package timgmbh.monopoly.modules;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import timgmbh.monopoly.general.Config;
import timgmbh.monopoly.objects.User;
import timgmbh.monopoly.services.DatabaseService;
import timgmbh.monopoly.services.ParserService;

public class Leaderboard extends BaseModule {
	public void execute() {
		ParserService.setTitle("Leaderboard");
		ParserService.addHeader("<link rel=\"stylesheet\" href=\"" + Config.getCssPath() + "leaderboard.css\">");
		
		ParserService.addContent("<center><table class=\"leaderboard\"><tr><th><u><center>Nummer</center></u></th><th><u><center>Spieler</center></u></th><th><u><center>Siege</center></u></th></tr></center>");
		
		PreparedStatement stmnt = null;
		ResultSet res = null;
		try {
			stmnt = DatabaseService.getConn().prepareStatement("SELECT ID FROM USER ORDER BY VICTORIES DESC, USERNAME ASC");
			res = stmnt.executeQuery();
		
			User temp = new User();
			int i = 1;
			while (res.next()) {
				temp.load(res.getInt("ID"));
				
				ParserService.addContent(""
					+ "		<center><tr>"
					+ "			<td>" + i + "</td>"
					+ "			<td>" + temp.getUsername() + "</td>"
					+ "			<td>" + temp.getVictories() + "</td>"
				);
				
				i++;
			}
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
		} finally {
			try {
				res.close();
				stmnt.close();
			} catch (Exception e) {
				System.out.println("Error: " + e.getClass() + ": " + e.getMessage());
			}
		}
		
		ParserService.addContent("</table></center>");
	}
}
